﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHTU;
using DevComponents.DotNetBar;

namespace NOMFinalSettlement.AppForm.Program
{
    public partial class fAddNewProfile : Office2007Form
    {
        FinalSettlementModel.TableHoSo profileDB = null;
        FinalSettlementModel.TableNhomCongTrinh groupProject = null;
        FinalSettlementModel.TableQuyetDinhDauTu acceptInvestment =null;
        FinalSettlementModel.TableChuDauTu investor = null;
        FinalSettlementModel.TableHinhThucChonThau construct = null;

        int idSelected = -1;

        public fAddNewProfile()
        {
            InitializeComponent();
        }

        private void fAddNewProfile_Load(object sender, EventArgs e)
        {
            doEnableControl(false);
            btnSave.Enabled = false;
            btnDelete.Enabled = false;

            // Khởi tạo lớp model của TableNhomCongTrinh
            groupProject = new FinalSettlementModel.TableNhomCongTrinh();
            groupProject.EventLoadData += new FinalSettlementModel.TableNhomCongTrinh.LoadData(OnLoadGroupProject);
            doLoadDataComboboxGroupProject();

            // Khởi tạo lớp model của TableQuyetDinhDauTu
            acceptInvestment = new FinalSettlementModel.TableQuyetDinhDauTu();
            acceptInvestment.EventLoadData += new FinalSettlementModel.TableQuyetDinhDauTu.LoadData(OnLoadAcceptInvestment);
            doLoadDataComboboxAcceptInvestment();

            // Khởi tạo lớp model của TableChuDauTu
            investor= new FinalSettlementModel.TableChuDauTu();
            investor.EventLoadData += new FinalSettlementModel.TableChuDauTu.LoadData(OnLoadInvestor);
            doLoadDataComboboxInvestor();

            // Khởi tạo lớp model của TableHinhThucChonThau
            construct = new FinalSettlementModel.TableHinhThucChonThau();
            construct.EventLoadData += new FinalSettlementModel.TableHinhThucChonThau.LoadData(OnLoadConstructName);
            doLoadDataComboboxConstructName();

            // Khởi tạo lớp model của TblHoSo
            profileDB = new FinalSettlementModel.TableHoSo();
            profileDB.EventLoadData += new FinalSettlementModel.TableHoSo.LoadData(OnLoadProfileData);
            doLoadData();            
        }

        void OnLoadProfileData()
        {
            ISynchronizeInvoke i = (ISynchronizeInvoke)this;
            if (i.InvokeRequired)
            {
                FinalSettlementModel.TableHoSo.LoadData tmpDelegate = new FinalSettlementModel.TableHoSo.LoadData(OnLoadProfileData);
                i.BeginInvoke(tmpDelegate, null);
                return;
            }
            //Hàm xử lý
            doLoadData();
        }
        void OnLoadGroupProject()
        {
            ISynchronizeInvoke i = (ISynchronizeInvoke)this;
            if (i.InvokeRequired)
            {
                FinalSettlementModel.TableNhomCongTrinh.LoadData tmpTableNhomCongTrinh = new FinalSettlementModel.TableNhomCongTrinh.LoadData(OnLoadGroupProject);
                i.BeginInvoke(tmpTableNhomCongTrinh, null);
                return;
            }
            doLoadDataComboboxGroupProject();
        }

        void OnLoadAcceptInvestment()
        {
            ISynchronizeInvoke i = (ISynchronizeInvoke)this;
            if (i.InvokeRequired)
            {
                FinalSettlementModel.TableQuyetDinhDauTu.LoadData tmpTableQuyetDinhDauTu = new FinalSettlementModel.TableQuyetDinhDauTu.LoadData(OnLoadAcceptInvestment);
                i.BeginInvoke(tmpTableQuyetDinhDauTu, null);
                return;
            }
            doLoadDataComboboxAcceptInvestment();
        }

        void OnLoadInvestor()
        {
            ISynchronizeInvoke i = (ISynchronizeInvoke)this;
            if (i.InvokeRequired)
            {
                FinalSettlementModel.TableChuDauTu.LoadData tmpTableChuDauTu = new FinalSettlementModel.TableChuDauTu.LoadData(OnLoadInvestor);
                i.BeginInvoke(tmpTableChuDauTu, null);
                return;
            }
            doLoadDataComboboxInvestor();
        }
        void OnLoadConstructName()
        {
            ISynchronizeInvoke i = (ISynchronizeInvoke)this;
            if (i.InvokeRequired)
            {
                FinalSettlementModel.TableHinhThucChonThau.LoadData tmpTableHinhThucChonThau = new FinalSettlementModel.TableHinhThucChonThau.LoadData(OnLoadConstructName);
                i.BeginInvoke(tmpTableHinhThucChonThau, null);
                return;
            }
            doLoadDataComboboxConstructName();
        }

        #region Sự kiện
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (idSelected > 0)
            {
                doDelete();
                doReloadControl();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!superValidator1.Validate())
            {
                MsgBox.Show("Vui lòng nhập đầy đủ các thông tin yêu cầu.", "Thông báo", NHTU.MsgBoxButton.Ok, NHTU.MsgBoxIcon.Stop);
                return;
            }
            
            if (idSelected < 1)
            {
                doSave();
            }
            else
            {
                doUpdate();
            }            
            doReloadControl();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            superValidator1.ClearFailedValidations();
            doEnableControl(true);
            doReloadControl();
            btnSave.Enabled = true;
        }

        private void dataViewNewDoc_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            idSelected = MainApp.Self.appProcess.getIdGrid(dataViewNewDoc, e, ctMenu);
            if (idSelected < 1)
            {
                doEnableControl(false);
                btnSave.Enabled = false;
                btnDelete.Enabled = false;
                doReloadControl();
            }
            else
            {
                btnSave.Enabled = true;
                btnDelete.Enabled = true;
                doEnableControl(true);
                //Lấy dữ liệu từ grid ra text box
                DataGridViewSelectedRowCollection selectRows = dataViewNewDoc.SelectedRows;
                foreach (DataGridViewRow row in selectRows)
                {
                    if (row.Cells[0].Value != null)
                    {
                        txtProjectName.Text = row.Cells[1].Value.ToString();
                        txtProjectCode.Text = row.Cells[2].Value.ToString();
                        cbbGroupProject.Text = row.Cells[3].Value.ToString();
                        cbbAcceptInvestment.Text = row.Cells[4].Value.ToString();
                        cbbInvestor.Text = row.Cells[5].Value.ToString();
                        dateAddDoc.Text = row.Cells[6].Value.ToString();
                        totalProject.Text = row.Cells[7].Value.ToString();
                        totalEstimates.Text = row.Cells[8].Value.ToString();
                        totalSettlement.Text = row.Cells[9].Value.ToString();
                        cbbConstructName.Text = row.Cells[10].Value.ToString();
                    }
                }
            }
        }

        private void menuEdit_Click(object sender, EventArgs e)
        {

        }

        private void menuDelete_Click(object sender, EventArgs e)
        {
            if (idSelected > 0)
            {
                doDelete();
                doReloadControl();
            }
        }

        #endregion

        #region "Hàm xử lý"        
        //Load data vào gridview
        void doLoadData()
        {
            dataViewNewDoc.Rows.Clear();
            DataTable dt = profileDB.ListNewProfileData();

            foreach (DataRow row in dt.Rows)
            {
                if (dataViewNewDoc.ColumnCount > 0)
                {
                    dataViewNewDoc.Rows.Add(row["ID"], row["TenDuAn"], row["MaDuAn"], row["NhomCongTrinh"], row["QuyetDinhDauTu"], row["ChuDauTu"],
                        row["NgayNhan"], row["TongDuAn"], row["TongDuToan"], row["DeNghiQuyetToan"], row["HinhThucChonThau"], row["Fullname"]);
                }
            }
        }
        //Data to Commbobox
        void doLoadDataComboboxGroupProject()
        {            
            cbbGroupProject.DataSource = groupProject.ListGroupObjectData();
            cbbGroupProject.DisplayMember = "NhomCongTrinh";
            cbbGroupProject.ValueMember = "ID";
        }
        void doLoadDataComboboxAcceptInvestment()
        {
            cbbAcceptInvestment.DataSource = acceptInvestment.ListInvestmentData();
            cbbAcceptInvestment.DisplayMember = "QuyetDinhDauTu";
            cbbAcceptInvestment.ValueMember = "ID";
        }
        void doLoadDataComboboxInvestor()
        {
            cbbInvestor.DataSource = investor.ListInvestorData();
            cbbInvestor.DisplayMember = "ChuDauTu";
            cbbInvestor.ValueMember = "ID";
        }
        void doLoadDataComboboxConstructName()
        {
            cbbConstructName.DataSource = construct.ListBidData();
            cbbConstructName.DisplayMember = "HinhThucChonThau";
            cbbConstructName.ValueMember = "ID";
        }

        void doSave()
        {
            if (profileDB.checkProfileName(-1, txtProjectName.Text))
            {
                profileDB.insertProfile(txtProjectName.Text, 
                                        txtProjectCode.Value, 
                                        (int)cbbGroupProject.SelectedValue, 
                                        (int)cbbAcceptInvestment.SelectedValue, 
                                        (int)cbbInvestor.SelectedValue, 
                                        dateAddDoc.Value, 
                                        (decimal)totalProject.Value, 
                                        (decimal)totalEstimates.Value, 
                                        (decimal)totalSettlement.Value, 
                                        (int)cbbConstructName.SelectedValue, 
                                        MainApp.Self.DBUSER.ID);
            }
            else
            {
                MsgBox.Show("Dữ liệu này đã tồn tại. Vui lòng kiểm tra lại!", "Thông Báo", MsgBoxButton.YesNo, MsgBoxIcon.Stop);
            }
        }

        void doUpdate()
        {
            if (profileDB.checkProfileName(idSelected, txtProjectName.Text))
            {
                profileDB.updateProfile(idSelected, 
                                        txtProjectName.Text,
                                        txtProjectCode.Value,
                                        (int)cbbGroupProject.SelectedValue,
                                        (int)cbbAcceptInvestment.SelectedValue,
                                        (int)cbbInvestor.SelectedValue,
                                        dateAddDoc.Value,
                                        (decimal)totalProject.Value,
                                        (decimal)totalEstimates.Value,
                                        (decimal)totalSettlement.Value,
                                        (int)cbbConstructName.SelectedValue,
                                        MainApp.Self.DBUSER.ID);
            }
            else
            {
                MsgBox.Show("Dữ liệu này đã tồn tại. Vui lòng kiểm tra lại!", "Thông Báo", MsgBoxButton.YesNo, MsgBoxIcon.Stop);
            }
        }

        void doDelete()
        {
            if (MsgBox.Show("Bạn có chắc chắn muốn xoá dữ liệu này không ?", "Thông Báo",
                 MsgBoxButton.YesNo, MsgBoxIcon.Question) == MsgBoxResult.Yes)
            {
                profileDB.deleteProfile(idSelected);
            }
        }
        #endregion

        # region Hàm dùng chung
        void doReloadControl()
        {
            txtProjectName.Text = "";
            txtProjectName.Focus();
            txtProjectCode.Text = null;
            cbbGroupProject.Text = "";
            cbbAcceptInvestment.Text = "";
            cbbInvestor.Text = "";
            dateAddDoc.Text = "";
            totalProject.Text = null;
            totalEstimates.Text = null;
            totalSettlement.Text = null;
            cbbConstructName.Text = "";
        }

        void doEnableControl(bool yes)
        {
            txtProjectName.Enabled = yes;
            txtProjectCode.Enabled = yes;
            cbbGroupProject.Enabled = yes;
            cbbAcceptInvestment.Enabled = yes;
            cbbInvestor.Enabled = yes;
            dateAddDoc.Enabled = yes;
            totalProject.Enabled = yes;
            totalEstimates.Enabled = yes;
            totalSettlement.Enabled = yes;
            cbbConstructName.Enabled = yes;
        }
        #endregion

        
    }
}
